<template>
	<view class="page-wrap">
		<view class="userInfo">
			<view class="content">
				<image class="avtor" @tap="loginOut" src="/static/user.png"></image>
				<view class="infos">
					<view class="name">嗨！{{ userInfo.name || userInfo.username }}-{{ userInfo.job }}</view>
					<view class="class">项目组：{{ userInfo.team }}</view>
					<view class="status">
						<text class="status">状态 </text>
						<text class="status-txt"> {{ workMap[userInfo.worker_status] }}</text>
					</view>
				</view>
			</view>
		</view>
		<view class="statistics">
			<view class="job-total">
				<view class="item">
					<!-- <img src="/static/home_content_bg.png" class="bg" alt=""> -->
					<view class="item-title">
						总项目
					</view>
					<view class="num-box">
						<view class="num">{{ totol.p_w_num || 0 }}</view>
						<image class="num-icon" src="/static/home_content_icon1.png"></image>
					</view>
				</view>
				<view class="item">
					<!-- <img src="/static/home_content_bg.png" class="bg" alt=""> -->
					<view class="item-title">
						已完成
					</view>
					<view class="num-box">
						<view class="num">{{ totol.p_c_num || 0 }}</view>
						<image class="num-icon" src="/static/home_content_icon2.png"></image>
					</view>
				</view>
				<view class="item">
					<!-- <img src="/static/home_content_bg.png" class="bg" alt=""> -->
					<view class="item-title">
						未完成
					</view>
					<view class="num-box">
						<view class="num">{{ totol.p_n_num || 0 }}</view>
						<image class="num-icon" src="/static/home_content_icon3.png"></image>
					</view>
				</view>
			</view>
			<view class="job-list">
				<view class="item" @tap="getJobList('巡检单','1', '/pages/patrol/patrol')">
					<view class="num">{{ totol.patrol_num || 0 }}</view>
					<view class="label">巡检单</view>
				</view>
				<view class="item" @tap="getJobList('维修单','2', '/pages/fix/fix')">
					<view class="num">{{ totol.maintain_num || 0 }}</view>
					<view class="label">维修单</view>
				</view>
				<view class="item" @tap="getJobList('培训单','4', '/pages/training/training')">
					<view class="num">{{ totol.train_num || 0 }}</view>
					<view class="label">培训单</view>
				</view>
				<view class="item" @tap="getJobList('隐患单','3', '/pages/problem/problem')">
					<view class="num">{{ totol.pitfall_num || 0 }}</view>
					<view class="label">隐患单</view>
				</view>
				<view class="item" @tap="getJobList('测试单','6', '/pages/test/test')">
					<view class="num">{{ totol.test_num || 0 }}</view>
					<view class="label">测试单</view>
				</view>
				<view class="item" @tap="getJobList('拆迁单','5', '/pages/move/move')">
					<view class="num">{{ totol.migrate_num || 0 }}</view>
					<view class="label">拆迁单</view>
				</view>
			</view>
		</view>
		<view class="list-box-title">
			<view class="title">任务列表</view>
			<view class="more">
				<view class="item" @tap="init()">
					<image class="icon" src="/static/dingwei_icon2.png"></image>
					<view class="txt">定位</view>
				</view>
				<view class="item" @tap="jump('/pages/jobCenter/jobCenter')">
					<image class="icon" src="/static/sousuo_icon.png"></image>
					<view class="txt">查询</view>
				</view>
			</view>
		</view>

		<view class="list-data-box">
			<view class="list-data" v-for="(item, index) in works" :key="index">
				<view class="baseInfo">
					<view class="address">
						<view class="label">{{ item.name }}</view>
						<view class="poi" v-if="item.km">
							<image class="icon" src="/static/dingwei_icon1.png"></image>
							<view class="txt">{{ item.km }}</view>
						</view>
					</view>
					<view class="address-detail">
						{{ item.address }}
					</view>
					<view class="daohan">
						<view class="type">
							<view class="label">类型：</view>
							<view class="txt">{{ userWokTypeMap[item.type].label }}</view>
						</view>
						<view class="btn" @tap="getGPS(item.lat, item.lon)">导航去</view>
					</view>

				</view>
				<view class="btns">
					<view class="item" @tap="jump('/pages/information/information?id=' + item.id)">资料更新</view>
					<view class="item active" @tap="openAlert(item)">进入工单</view>
				</view>
			</view>


		</view>

		<PopView :title="'工单列表'" :popShow="popShow" @popClose="popClose" @popOpen="popOpen">
			<template slot="content">
				<view class="poplist pop-content">
					<view class="item" v-for="(item, index) in jobMap" :key="index" @click="toJob(item.path)">
						<view class="item-job">
							<text style="z-index: 10;position: relative;">{{ item.label }}</text>
							<text class="item-job-bj"></text>
						</view>
					</view>
				</view>
			</template>
		</PopView>

		<PopView :title="currentTitle" :popShow="popShow2" @popClose="popClose2" @popOpen="popOpen2">
			<template slot="content">
				<view class="poplist pop-content">
					<view class="item" v-for="(item, index) in currentJobList" :key="index" @click="toJob2(item)">
						<view class="item-job" style="width: 100%;">
							<view style="z-index: 10;position: relative;display: flex; width: 100%; justify-content: space-between;">
								<view style="font-size: 24rpx;">{{ item.mname }}</view>
							    <text style="font-size: 20rpx;">{{ item.updated_at ||item.created_at }}</text>
							</view>
						</view>
					</view>
				</view>
			</template>
		</PopView>

	</view>
</template>

<script>
import { homeStatic, workerList, getArea, getCanWork, getMemberList ,getMonadList } from "@/api"

export default {
	data() {
		return {
			currentTitle:'',
			currentPath:'',
			currentJobList: [],
			memberList: [],
			popShow2: false,
			popShow: false,
			currentWork: null,
			jobMap: [
				{
					label: '巡检单-网点',
					//path: `/pages/patrol/patrol?type=1&id=${8}&`,
					path: `/pages/patrol/patrol`,
					type: '1'
				},
				{

					label: '巡检单-安防',
					//path: `/pages/patrol/patrol?type=3&id=${8}&`,
					path: `/pages/patrol/patrol`,
					type: '3'
				},
				{

					label: '巡检单-基站',
					//path: `/pages/patrol/patrol?type=2&id=${8}&`,
					path: `/pages/patrol/patrol`,
					type: '2'
				},

				{
					label: '维修单',
					path: '/pages/fix/fix?type=network'
				},

				{

					label: '培训单',
					path: '/pages/training/training?type=network'
				},

				{

					label: '隐患单',
					path: '/pages/problem/problem?type=network'
				},
				{

					label: '测试单',
					path: '/pages/test/test?'
				},

				{

					label: '拆迁单',
					path: '/pages/move/move?'
				},
			],
			workMap: {
				0: '在职',
				1: '离职'
			},
			works: [],
			totol: {
				p_w_num: 0,// 总项目数
				p_c_num: 0,// 总已完成数
				p_n_num: 0,//总未完成数
				patrol_num: 0,// 巡检单数量
				maintain_num: 0,// 维修单数量
				train_num: 0,// 培训单数量
				pitfall_num: 0,// 隐患单数量
				test_num: 0,// 测试单数量
				migrate_num: 0//迁拆单数量
			},
			// `type` 客户类型 1.网点 2.基站 3.安防',
			userWokTypeMap: {
				1: {
					label: '网点',
					type: 'network',
				},
				2: {
					label: '基站',
					type: 'basePoint'
				},
				3: {
					label: '安防',
					type: 'safe'
				}
			}
		}
	},
	async onLoad() {
		await this.getPosition()
		await this.getMemberList()
		await this.init()
	},
	async onShow() {
		await this.getPosition()
		await this.getMemberList()
		await this.init()
	},
	methods: {
		init() {
			wx.showLoading({
				title: '加载中',
				mask: true
			})
			Promise.all([
				this.getUserInfo(),
				this.homeStatic(),
				this.workerList(),
			]).finally(() => {
				setTimeout(() => {
					wx.hideLoading()
				}, 1000)
			})
		},
		popClose2() {
			this.popShow2 = false;
		},
		popOpen2() {
			this.popShow2 = true;
		},
		toJob2(item) {
            console.log(this.currentPath)
			this.jump(this.currentPath+`?type=${item.type}&id=${item.id}&mid=${item.mid}&pid=${item.pid}`, () => {
				this.popShow2 = false
			})
		},
		async getJobList(title,type, path) {
			this.currentTitle=title
			this.currentPath = path
			let res = await getMonadList({
				wid: this.wid,
				type
			})
			// currentJobList  接口  monad_list 取mid
			// memberlist  接口  member_list  取id
			this.currentJobList = res || []
			if(this.currentJobList.length==0){
			  return	wx.showToast({
					title:'没有该类型的工单记录',
					icon:'none'
				})
			}
			this.popShow2 = true
		},
		async getArea() {
			await getArea({})
		},
		async homeStatic() {
			let res = await homeStatic({ wid: this.wid })
			this.totol = res
		},
		async workerList() {
			let res = await workerList({
				wid: this.wid,
				type: 2,
				rid: '',
				vague: ''
			})
			let arr = []
			Object.keys(res.list).forEach((key) => {
				arr.push(res.list[key])
			})
			let latitude = wx.getStorageSync("latitude");
			let longitude = wx.getStorageSync("longitude");
			this.works = arr.map(v => {
				return {
					...v,
					km: this.getDistance(latitude, longitude, v.lat, v.lon),
					distance:this.getDistance2(latitude, longitude, v.lat, v.lon),
				}
			}).sort((a,b)=>a.distance-b.distance)

			console.log(this.works,222)
		},
		async openAlert(item, type = 0) {
			this.currentWork = item
			let params = {
				pid: item.pid,
				wid: this.wid,
				mid: item.id,
				type: item.type
			}
			let res = await getCanWork(params)
			let arr = [];
			// 巡检单-网点
			if (res.patrol_state == 1) {
				//  if (true) {
				arr.push({
					label: '巡检单',
					path: `/pages/patrol/patrol?type=${item.type}&id=${res.patrol_id}&mid=${item.id}&pid=${item.pid}`,
					type: item.type
				})

			}
			// 巡检单-安防
			//if (res.patrol_state == 1 && res.patrol_id && item.type == 3) {
			// if (true) {
			// 	arr.push({
			// 		label: '巡检单-安防',
			// 		path: `/pages/patrol/patrol?type=${item.type}&id=${res.patrol_id}&mid=${item.id}&pid=${item.pid}`,
			// 		type: '3'
			// 	})
			// }
			//巡检单-基站
			//if (res.patrol_state == 1 && res.patrol_id && item.type == 2) {
			// if (true) {
			// 	arr.push({
			// 		label: '巡检单-基站',
			// 		path: `/pages/patrol/patrol?type=${item.type}&id=${res.patrol_id}&mid=${item.id}&pid=${item.pid}`,
			// 		type: '2'
			// 	})
			// }
			// 测试
			if (res.test_state == 1) {
				// if (true) {
				arr.push({
					label: '测试单',
					path: `/pages/test/test?type=${item.type}&id=${res.test_id}&mid=${item.id}&pid=${item.pid}`,
					type: item.type
				})
			}
			// 培训
			if (res.train_state == 1) {
				// if (true) {
				arr.push({
					label: '培训单',
					path: `/pages/training/training?type=${item.type}&id=${res.train_id}&mid=${item.id}&pid=${item.pid}`,
					type: item.type
				})
			}
			// 迁拆
			if (res.migrate_state == 1) {
				// if (true) {
				arr.push({
					label: '迁拆单',
					path: `/pages/move/move?type=${item.type}&id=${res.migrate_id}&mid=${item.id}&pid=${item.pid}`,
					type: item.type
				})
			}

			// 维修
			if (res.maintain_state == 1) {
				// if (true) {
				arr.push({
					label: '维修单',
					path: `/pages/fix/fix?type=${item.type}&id=${res.maintain_id}&mid=${item.id}&pid=${item.pid}`,
					type: item.type
				})
			}

			// 隐患
			if (res.pitfall_state == 1) {
				// if (true) {
				arr.push({
					label: '隐患单',
					path: `/pages/problem/problem?type=${item.type}&id=${res.pitfall_id}&mid=${item.id}&pid=${item.pid}`,
					type: item.type
				})
			}

			// if (arr.length == 0) {
			// 	return wx.showToast({
			// 		title: '该任务没有可执行的工单，请发布任务工单',
			// 		icon: 'none'
			// 	})
			// }
			this.jobMap = arr
			this.popShow = true
		},
		toJob(path) {
			this.jump(path, () => {
				this.popShow = false
			})
		},
		loginOut() {
			let _this = this
			wx.showModal({
				title: '用户登出',
				content: '您确定是否登出？',
				success(res) {
					if (res.confirm) {
						wx.clearStorageSync();
						_this.init()
					} else if (res.cancel) {
						console.log('用户点击取消')
					}
				}
			})
		},
		async getMemberList() {
			let res = await getMemberList({
				wid: this.wid
			})
			console.log(res, 11111)
			this.memberList = res || []
		}

	}
}
</script>

<style src="./assets/index.scss" lang="scss" scoped></style>


